package com.jwdf.uda.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */
public class LikeThumbnailView extends FrameLayout {
    private static  int MAX_THUMBNAIL = 5;
    private LinearLayout mContainerLayout;
    private OnMoreUserClickListener mMoreUserClickListener;

    public LikeThumbnailView(Context context) {
        super(context);
        initViews(context);
    }

    public LikeThumbnailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    private void initViews(Context context) {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mContainerLayout = new LinearLayout(context);
        mContainerLayout.setOrientation(LinearLayout.HORIZONTAL);
        this.addView(mContainerLayout, params);
    }

    public void setMoreUserClickListener(OnMoreUserClickListener listener) {
        if (listener != null) {
            mMoreUserClickListener = listener;
        }
    }

    public void setMaxThumbnail(int i){
        MAX_THUMBNAIL=i;
    }

    public void bindView(final List<PbBean.PbUserSimple> userSimples, final OnUserAvatarClickListener listener) {
        mContainerLayout.removeAllViews();
        final Context context = getContext();
        LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (userSimples.size() > MAX_THUMBNAIL) {
            for (int i = 0; i < MAX_THUMBNAIL; i++) {
                final UserItem item = createItem(context);
                final PbBean.PbUserSimple userSimple = userSimples.get(i);
                item.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onClick(v.getContext(), userSimple.getUserId(), userSimple.getIsDesigner());
                        }
                    }
                });
                mContainerLayout.addView(item, itemParams);
                item.bindView(userSimple);
            }
            MoreUserItem moreUserItem = createMoreItem(context);
            moreUserItem.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mMoreUserClickListener != null) {
                        mMoreUserClickListener.onClick(v.getContext());
                    }
                }
            });
            mContainerLayout.addView(moreUserItem, itemParams);
        } else {
            for (final PbBean.PbUserSimple userSimple : userSimples) {
                final UserItem item = createItem(context);
                item.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onClick(v.getContext(), userSimple.getUserId(), userSimple.getIsDesigner());
                        }
                    }
                });
                mContainerLayout.addView(item, itemParams);
                item.bindView(userSimple);
            }
        }
    }

    private UserItem createItem(Context context) {
        UserItem item = new UserItem(context);
        return item;
    }

    private MoreUserItem createMoreItem(Context context) {
        MoreUserItem item = new MoreUserItem(context);
        return item;
    }

    private static class MoreUserItem extends UserItem {
        public MoreUserItem(Context context) {
            super(context);
            avatarFl.setVisibility(GONE);
            ivAvatarMore.setVisibility(VISIBLE);
        }
    }

    private static class UserItem extends RelativeLayout {
        CircleImageView ivAvatar;
        ImageView ivDesigner, ivAvatarMore;
        FrameLayout avatarFl;

        public UserItem(Context context) {
            super(context);
            LayoutInflater.from(getContext()).inflate(R.layout.item_reply_user_like, this, true);
            ivAvatar = (CircleImageView) findViewById(R.id.iv_avatar);
            ivAvatarMore = (ImageView) findViewById(R.id.iv_avatar_more);
            ivDesigner = (ImageView) findViewById(R.id.iv_designer);
            avatarFl = (FrameLayout) findViewById(R.id.fl_avatar);
        }

        public void bindView(PbBean.PbUserSimple userSimple) {
            ImageLoader.getInstance().displayImage(userSimple.getAvatar().getUrl(), ivAvatar);
            ivDesigner.setVisibility(userSimple.getIsDesigner() ? VISIBLE : GONE);
        }
    }

    public interface OnUserAvatarClickListener {
        void onClick(Context context, String userId, boolean isDesigner);
    }

    public interface OnMoreUserClickListener {
        void onClick(Context context);
    }

}
